<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WeChatController extends Controller
{
    /**
     * 获取微信 JSSDK 配置
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function jssdk(Request $request)
    {
        /** @var \EasyWeChat\OfficialAccount\Application $app */
        $app = app('wechat.official_account');

        if ($url = $request->get('url')) {
            $app->jssdk->setUrl(urldecode($url));
        }

        $config = $app->jssdk->buildConfig(
            (array) $request->get('APIs', []),
            $request->get('debug') == 'true',
            $request->get('beta') == 'true',
            false
        );

        return response()->json(['code' => '200', 'message' => 'success', 'data' => $config]);
    }
}
